package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.entity.*;
import com.fs.mapper.*;
import com.fs.service.IAdministratorsService;
import com.fs.service.IMaterialsService;

import com.fs.service.IReviewersService;
import com.fs.util.AutoReview;

import com.fs.util.Seal;
import com.fs.util.StudentsContext;
import com.fs.util.Wordrecognize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-12
 */
@Service
public class MaterialsServiceImpl implements IMaterialsService {
    @Autowired
    MaterialsMapper materialsMapper;
    @Autowired
    MaterialcategoryMapper materialcategoryMapper;
    @Autowired
    MaterialallocationMapper materialallocationMapper;
    @Autowired
    MaterialreviewMapper materialreviewMapper;
    @Autowired
    SysconfigMapper systconfigMapper;
    @Autowired
    ReviewersMapper reviewersMapper;
    @Autowired
    IAdministratorsService administratorsService;
    @Autowired
    StudentsMapper studentsMapper;
    @Autowired
    Seal seal;

    @Override
    public String handle(Materials material) {
        material.setState(1);
        Boolean res = materialsMapper.insert(material)>0;
        int id = material.getMid();
        System.out.println("id:"+id);
        Sysconfig sysconfig = systconfigMapper.selectById(1);
        //自动审核（如果启用自动审核并且材料可以自动审核）
        if(materialcategoryMapper.selectById(material.getMcid()).getAutoenable()==1&&sysconfig.getReviewmodel()==1){
            String imageseal =  seal.seal(material.getMimage(),"seal");
            String wordseal = Wordrecognize.wordrecognize(material.getMimage());
            //检测不到用户的名字
            if (!wordseal.contains(studentsMapper.selectById(material.getSid()).getSname())){
                material.setState(3);
                updateMaterialresult(material,1,"材料无法证明是本人",3);
                return "材料无法证明是本人";
            }
            //检测不到印章
            if (!imageseal.contains("location")){
                material.setState(3);
                updateMaterialresult(material,1,"材料无法认定学分",3);
                return "材料无法认定学分";
            }
            //如果是四六级文件则检查二维码
            if(material.getMcid()==33&&!seal.seal(material.getMimage(),"qrcode").contains("QR_CODE")){
                material.setState(3);
                updateMaterialresult(material,1,"材料不完整",3);
                return "材料不完整";
            }
            material.setState(4);
            updateMaterialresult(material,1,"审核通过",4);
        }
        if (sysconfig.getAllocationmodel()==1){
            List<ReviewersShow>  reviewers= (List<ReviewersShow>) administratorsService.ShowReviewers().getData();
            reviewers.sort(new Comparator<ReviewersShow>() {
                @Override
                public int compare(ReviewersShow o1, ReviewersShow o2) {
                    return o1.getAll()-o2.getAll();
                }
            });
            Materialallocation materialallocation = new Materialallocation();
            materialallocation.setMid(material.getMid());
            materialallocation.setRid(reviewers.get(0).getRid());
            materialallocation.setDate(LocalDateTime.now());
            materialallocationMapper.insert(materialallocation);
        }
        return "操作成功！";
    }

    @Override
    public Materials getMaterialDetail(int mid) {
        return materialsMapper.selectById(mid);
    }

    @Override
    public boolean withdrawMaterial(int mid) {
        if (materialsMapper.withdrawMaterial(mid)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateMaterial(Materials materials) {
        return materialsMapper.updateMaterial(materials)>0;
    }

    @Override
    public boolean save(Materials materials) {
        return materialsMapper.save(materials)>0;
    }

    @Override
    public boolean manual(int mid) {
        return materialsMapper.manual(mid)>0;
    }

    @Override
    public Page queryByPageAndCondition(MaterialsCondition materialsCondition) {

        Page<MaterialsAIShowInt> page=Page.of(1,6);
        if(materialsCondition.getCurrentpage()!=null)
        {
            page.setCurrent(materialsCondition.getCurrentpage());
        }
        Page<MaterialsAIShowInt> matPage=materialsMapper.selectMaterialsPageWithConditions(page,materialsCondition);
        List<MaterialsAIShow> materialsAIShows=new ArrayList<>();
        List<MaterialsAIShowInt> materialsAIShowInts=matPage.getRecords();
        for(MaterialsAIShowInt materialsAIShowInt:materialsAIShowInts)
        {
            MaterialsAIShow materialsAIShow=new MaterialsAIShow();
            materialsAIShow.setMid(materialsAIShowInt.getMid());
            materialsAIShow.setSname(materialsAIShowInt.getSname());
            materialsAIShow.setMcaname(materialsAIShowInt.getMcaname());
            materialsAIShow.setMgrade(materialsAIShowInt.getMgrade());
            materialsAIShow.setMcredit(materialsAIShowInt.getMcredit());
            materialsAIShow.setMname(materialsAIShowInt.getMname());
            materialsAIShow.setMimage(materialsAIShowInt.getMimage());
            materialsAIShow.setMattimage(materialsAIShowInt.getMattimage());
            materialsAIShow.setMdescrib(materialsAIShowInt.getMdescrib());
            materialsAIShow.setDatetime(materialsAIShowInt.getDatetime());
            materialsAIShow.setGrade(materialsAIShowInt.getGrade());
            materialsAIShow.setClassname(materialsAIShowInt.getClassname());
            materialsAIShow.setMajor(materialsAIShowInt.getMajor());
            materialsAIShow.setDepartmentname(materialsAIShowInt.getDepartmentname());
            if(materialsAIShowInt.getState()==2)
            {
                materialsAIShow.setState("未通过");
            }
            if(materialsAIShowInt.getState()==3)
            {
                materialsAIShow.setState("已通过");
            }
        }
        return matPage;
    }

    @Override
    public Page<MaterialsPepShowInt> selectPepMaterialsPageWithConditions(MaterialsCondition materialsCondition) {
        Page<MaterialsPepShowInt> page=Page.of(1,6);
        if(materialsCondition.getCurrentpage()!=null)
        {
            page.setCurrent(materialsCondition.getCurrentpage());
        }
        Page<MaterialsPepShowInt> matPage=materialsMapper.selectPepMaterialsPageWithConditions(page,materialsCondition);
        List<MaterialsPepShow> materialsPepShows=new ArrayList<>();
        List<MaterialsPepShowInt> materialsPepShowInts=matPage.getRecords();
        for(MaterialsPepShowInt materialsPepShowInt:materialsPepShowInts)
        {
            MaterialsPepShow materialsPepShow=new MaterialsPepShow();
            materialsPepShow.setMid(materialsPepShowInt.getMid());
            materialsPepShow.setSname(materialsPepShowInt.getSname());
            materialsPepShow.setMcaname(materialsPepShowInt.getMcaname());
            materialsPepShow.setMgrade(materialsPepShowInt.getMgrade());
            materialsPepShow.setMcredit(materialsPepShowInt.getMcredit());
            materialsPepShow.setMname(materialsPepShowInt.getMname());
            materialsPepShow.setMimage(materialsPepShowInt.getMimage());
            materialsPepShow.setMattimage(materialsPepShowInt.getMattimage());
            materialsPepShow.setMdescrib(materialsPepShowInt.getMdescrib());
            materialsPepShow.setDatetime(materialsPepShowInt.getDatetime());
            materialsPepShow.setGrade(materialsPepShowInt.getGrade());
            materialsPepShow.setClassname(materialsPepShowInt.getClassname());
            materialsPepShow.setMajor(materialsPepShowInt.getMajor());
            materialsPepShow.setDepartmentname(materialsPepShowInt.getDepartmentname());
            if(materialsPepShowInt.getState()==5)
            {
                materialsPepShow.setState("未通过");
            }
            if(materialsPepShowInt.getState()==4)
            {
                materialsPepShow.setState("已通过");
            }
        }
        return matPage;
    }
    //审核，修改材料结果
    @Override
    public boolean updateMaterialresult(Materials materials,Integer rid,String detail,Integer statu) {
        QueryWrapper<Materials> queryWrapper=new QueryWrapper();
        queryWrapper.eq("mid",materials.getMid());
        Materialreview materialreview=new Materialreview();
        materialreview.setMid(materials.getMid());
        materialreview.setRid(rid);
        if(materials.getState()==4||materials.getState()==5)
        {
            materialreview.setMresult("已通过");
        }
        else {
            materialreview.setMresult("未通过");
        }
        materialreview.setMdetails(detail);
        materialreview.setMreviewtime(LocalDateTime.now());
        materialreview.setMrstatu(statu);
        System.out.println("才来奥lll"+materials);
        int rs=materialsMapper.update(materials,queryWrapper);
        int rs1=materialreviewMapper.insert(materialreview);
        if(rid!=null)
        {
            QueryWrapper<Materialallocation> materialallocationQueryWrapper=new QueryWrapper<>();
            materialallocationQueryWrapper.eq("rid",rid);
            materialallocationQueryWrapper.eq("mid",materials.getMid());
            Materialallocation materialallocation = materialallocationMapper.selectOne(materialallocationQueryWrapper);
            if(materialallocation!=null)
            {
                materialallocationMapper.delete(materialallocationQueryWrapper);
            }

        }
        if(rs!=0&&rs1!=0)
        {
            return true;
        }
        return false;
    }
}
